/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.auth.server.listener;

import com.iwindplus.auth.domain.event.LoginLogEvent;
import com.iwindplus.log.client.LoginLogClient;
import com.iwindplus.log.domain.dto.LoginLogDTO;
import jakarta.annotation.Resource;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 登录日志监听器.
 *
 * @author zengdegui
 * @since 2025/03/21 21:51
 */
@Slf4j
@Component
public class LoginLogListener {

    @Lazy
    @Resource
    private LoginLogClient loginLogClient;

    /**
     * 登录日志监听保存数据.
     *
     * @param loginLogEvent 登录日志事件
     */
    @Async
    @EventListener(LoginLogEvent.class)
    public void onApplicationEvent(LoginLogEvent loginLogEvent) {
        final LoginLogDTO logData = loginLogEvent.getLogData();
        log.info("登录日志监听保存数据={}", logData);
        if (Objects.isNull(logData)) {
            return;
        }
        this.loginLogClient.save(logData);
    }
}
